package com.pqc.tbk.service.admin;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.springframework.stereotype.Service;

import com.pqc.tbk.utils.Settings;

@Service
public class SystemServiceImpl implements SystemService {

	@Override
	public void updateProperties(final String system_website,
			final String system_name, final String system_icp,
			final String email_authuser, final String email_authpassword,
			final String email_smtphost, final String email_smtpport,
			final String taobao_appkey, final String taobao_appsecret,
			final String taobao_session, final String taobao_submiturl)
			throws Exception {

		final Properties config = new Properties();
		InputStream fis = null;
		OutputStream fos = null;
		final java.net.URL url = getClass().getResource("/settings.properties");
		final File file = new File(url.toURI());
		if (!file.exists()) {
			file.createNewFile();
		}
		fis = new FileInputStream(file);
		config.load(fis);
		fis.close();// 一定要在修改值之前关闭fis
		fos = new FileOutputStream(file);

		// system
		config.setProperty("system.website", system_website);
		config.setProperty("system.name", system_name);
		config.setProperty("system.icp", system_icp);
		// email
		System.out.println(email_authuser);
		config.setProperty("email.authuser", email_authuser);
		config.setProperty("email.authpassword", email_authpassword);
		config.setProperty("email.smtphost", email_smtphost);
		config.setProperty("email.smtpport", email_smtpport);
		// taobao
		config.setProperty("taobao.appkey", taobao_appkey);
		config.setProperty("taobao.appsecret", taobao_appsecret);
		config.setProperty("taobao.session", taobao_session);
		config.setProperty("taobao.submiturl", taobao_submiturl);
		System.out.println("fuck");
		config.store(fos, null);

		fos.close();
		fis.close();

		// 重新绑定
		bindSysProperties();
	}

	@Override
	public Map<String, String> convertToMap() {
		final Map<String, String> map = new HashMap<String, String>();

		map.put("system_website", Settings.System.website);
		map.put("system_name", Settings.System.name);
		map.put("system_icp", Settings.System.icp);
		map.put("email_authuser", Settings.Email.authuser);
		map.put("email_authpassword", Settings.Email.authpassword);
		map.put("email_smtphost", Settings.Email.smtphost);
		map.put("email_smtpport", Settings.Email.smtpport);
		map.put("taobao_appkey", Settings.Taobao.appkey);
		map.put("taobao_appsecret", Settings.Taobao.appsecret);
		map.put("taobao_session", Settings.Taobao.session);
		map.put("taobao_submiturl", Settings.Taobao.submiturl);
		return map;
	}

	@Override
	public void bindSysProperties() throws Exception {
		final InputStream input = getClass().getResourceAsStream(
				"/settings.properties");
		final Properties config = new Properties();
		config.load(input);
		System.out.println(config.getProperty("system.name"));
		// system
		Settings.System.website = config.getProperty("system.website");
		Settings.System.name = config.getProperty("system.name");
		Settings.System.icp = config.getProperty("system.icp");
		// email
		Settings.Email.authuser = config.getProperty("email.authuser");
		Settings.Email.authpassword = config.getProperty("email.authpassword");
		Settings.Email.smtphost = config.getProperty("email.smtphost");
		Settings.Email.smtpport = config.getProperty("email.smtpport");
		// taobao
		Settings.Taobao.appkey = config.getProperty("taobao.appkey");
		Settings.Taobao.appsecret = config.getProperty("taobao.appsecret");
		Settings.Taobao.session = config.getProperty("taobao.session");
		Settings.Taobao.submiturl = config.getProperty("taobao.submiturl");

		input.close();

	}
}
